package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.UserLike;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

import static java.util.stream.Collectors.toList;


@Slf4j
@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId)
                    .and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            log.info("数据库中{}喜欢用户{}的数据", userLike == null ? "已经存在" : "还不存在", likeUserId);
            //2、如果不存在，保存
            if (userLike == null) {
                log.info("不存在，保存");
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                log.info("存在，更新");
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<UserLike> getFriend(int code, Long userId) {
        log.info("查询好友,code = {}", code);
        Query query = new Query();
        Query query2 = new Query();
        String title = "";
        switch (code) {
            case Constants.LIKE_YOU://我喜欢的
                title = "LIKE_YOU";
                query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
                break;
            case Constants.LIKE_ME://喜欢我的
                title = "LIKE_ME";
                query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
                break;
            case Constants.LIKE_MUTUAL:
                query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
                query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
                List<UserLike> list1 = mongoTemplate.find(query, UserLike.class);
                List<UserLike> list2 = mongoTemplate.find(query2, UserLike.class);

                List<UserLike> collect = list1.stream().filter(list2::contains).collect(toList());
                log.info("LIKE_MUTUAL 查询结果数据userlike = {}", collect);
                return collect;
        }
        List<UserLike> userLike = mongoTemplate.find(query, UserLike.class);
        log.info("{}查询结果数据userlike = {}",title, userLike);
        return userLike;
    }

    @Override
    public Boolean delectLike(Long likeUserId,Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId).and("isLike").is(true));
        List<UserLike> userLike = mongoTemplate.find(query, UserLike.class);
        log.info("userLike == {}",userLike);
        List<UserLike> articles = mongoTemplate.findAllAndRemove(query, UserLike.class);
        if (CollUtil.isEmpty(articles)) {
            return false;
        }
        return true;
    }


}
